/**
 * @author: 小红
 * @date: 2025/4/1
 * @Description: 路由助手
 */
import type { RouteModuleType } from '@/router/types.ts';
import { type RouteRecordRaw, RouterView } from 'vue-router';

const dynamicRouteFiles = import.meta.glob('./routes/**/*.ts', {
	eager: true,
});



/**
 * 合并动态路由模块的默认导出
 * @param routeModules 动态导入的路由模块对象
 * @returns 合并后的路由配置数组
 */
function mergeRouteModules(routeModules: Record<string, unknown>): RouteRecordRaw[] {
	const mergedRoutes: RouteRecordRaw[] = [];

	for (const routeModule of Object.values(routeModules)) {
		const moduleRoutes = (routeModule as RouteModuleType)?.default ?? [];
		mergedRoutes.push(...moduleRoutes);
	}

	return mergedRoutes;
}

/**
 * 获取动态路由
 */
const routes = [
	{
		path: '/login',
		name: 'login',
		component: () => import('@/views/system/login/AppLogin.vue'),
	},
	{
		path: '',
		redirect: '/home',
	},
	{
		path: '/:pathMatch(.*)*', // 匹配所有未定义的路由
		redirect: '/home', // 重定向到 xxxx
	},
	{
		path: '/',
		name: 'each',
		component: () => import('@/layout/BaseLayout.vue'),
		children: [
			{
				name: 'sub',
				path: 'sub',
				component: RouterView,
			},
			...mergeRouteModules(dynamicRouteFiles),
		],
	},
];

export default routes;
